<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      autocomplete="on"
      label-position="left"
    >
      <div class="title-container">
        登录
      </div>
      <el-form-item prop="username" class="usernames">
        <span class="svg-container">
          <img src="@/assets/images/user.png"
        /></span>
        <el-input
          v-model="loginForm.username"
          placeholder="请输入用户名"
          type="text"
          ref="username"
        />
      </el-form-item>
      <el-form-item prop="password" class="usernames">
        <span class="svg-container"
          ><img src="@/assets/images/password.png"
        /></span>
        <el-input
          v-model="loginForm.password"
          placeholder="请输入密码"
          type="password"
          ref="password"
          :show-password="true"
          @keyup.enter.native="handleLogin"
        />
      </el-form-item>
      <el-button
        class="dllogin"
        :loading="loading"
        type="primary"
        @click.native.prevent="handleLogin"
        >登录</el-button
      >
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error('密码不能少于三位!'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入用户名'
          }
        ],
        password: [
          {
            required: true,
            trigger: 'blur',
            validator: validatePassword,
            message: '请输入密码'
          }
        ]
      },
      loading: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {},
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          var loginform = {
            username: this.loginForm.username,
            password: this.loginForm.password
          }
          this.$store
            .dispatch('user/login', loginform)
            .then(res => {
              if (res.code == 'OK') {
                this.$router.push({
                  path: this.redirect || '/',
                  query: this.otherQuery
                })
                this.loading = false
              } else {
                this.$message({
                  message: res.msg,
                  type: 'error'
                })
                this.loading = false
              }
            })
            .catch(err => {
              console.log(err)
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
/* reset element-ui css */

.login-container {
  .el-input {
    display: inline-block;
    height: 50px;
    width: calc(100% - 60px);
    background-color: #f2f4f9;
    margin-left: 15px;
    input {
      background-color: #f2f4f9;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #000000;
      height: 50px;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #ffffff inset !important;
        -webkit-text-fill-color: #000000 !important;
      }
    }
  }
  .el-form-item {
    width: 80%;
    margin: 0 auto 35px;
    box-sizing: border-box;
    height: 52px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    background-color: #fff;

    color: #454545;
    .el-form-item__content {
      vertical-align: middle;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background: url('../../assets/images/loginbg.png');
  .left {
    margin: 36px 0px 0px 67px;
    float: left;
    width: 50%;
  }
  .login-form {
    float: right;
    position: absolute;
    top: 50%;
    right: 222px;
    transform: translate(0, -50%);
    // width: 30%;
    // max-width: 100%;
    width: 523px;
    height: 610px;

    overflow: hidden;

    background-color: #fff;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 8px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 40px;
    height: 50px;
    line-height: 50px;
    // border-right: 1px solid #dde0eb;
    display: inline-block;
    img {
      display: inline-block;
    }
  }
  .svg-container:before {
    position: absolute;
    margin-top: 3px;
    margin-left: 43px;
    height: 26px;
    width: 1px;
    background: #cccccc;
    content: '';
  }

  .title-container {
    position: relative;
    font-family: pingfang;
    font-weight: 500;
    font-size: 26px;
    color: #000000;
    line-height: 30px;
    text-align: center;
    font-style: normal;
    text-transform: none;
    letter-spacing: 3px;
    margin: 75px 0;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }
  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
.usernames {
  background: #f2f4f9;
}
.dllogin {
  width: 379px;
  height: 54px;
  border-radius: 0;
  letter-spacing: 15px;

  margin: 100px auto;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  font-family: pingfang;
  font-weight: 500;
  font-size: 18px;
  color: #ffffff;
  line-height: 16px;
  // letter-spacing: 18px;
  text-align: center;
  font-style: normal;
  text-transform: none;
}
</style>
